package com.chapter.sample.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//增强类，内部提供增强方法
@Component
@Aspect
public class MyAdvice {

    //切入点表达式点抽取
    @Pointcut("execution(* com.chapter.sample.service.impl.*.*(..))")
    public void myPointcut(){}

    //@Before(value = "execution(* com.chapter.sample.service.impl.*.*(..))")
    public void beforeAdvice(){
        System.out.println("前置的增强......");
    }

    //@AfterReturning(value = "execution(* com.chapter.sample.service.impl.*.*(..))")
    public void afterReturningAdvice(){
        System.out.println("后置的增强......");
    }

    //@After(value = "execution(* com.chapter.sample.service.impl.*.*(..))")
    public void afterAdvice(){
        System.out.println("最终的增强......");
    }

    //@AfterThrowing(pointcut = "execution(* com.chapter.sample.service.impl.*.*(..))",throwing = "e")
    public void afterThrowingAdvice(Throwable e){
        System.out.println("异常抛出通知。。。抛出异常才执行.....");
    }

    @Around("MyAdvice.myPointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前的增强......");
        Object res = proceedingJoinPoint.proceed();//执行目标方法
        System.out.println("环绕后的增强......");
        return res;
//        System.out.println("最终的增强......");
    }
}
